package com.buybuyyi.stylistcrm.security.rest;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by yanwc on 2016/9/19.
 */
public class DefaultSimpleUrlAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        //生成accessToken
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        //返回服务端令牌及用户名称信息
        PrintWriter writer =  response.getWriter();
        JSONObject result = new JSONObject();
        result.put("success",false);
        result.put("message",exception.getMessage());
        writer.write(result.toJSONString());

    }
}
